package com.szcinda.service;

import com.szcinda.repository.OrderFollow;
import com.szcinda.repository.OrderFollowRepository;
import com.szcinda.repository.ZadOrder;
import com.szcinda.repository.ZadOrderRepository;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.Predicate;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Component
@EnableScheduling
public class ScheduleTaskService {

    private final ZadOrderRepository zadOrderRepository;
    private final OrderFollowRepository orderFollowRepository;

    public ScheduleTaskService(ZadOrderRepository zadOrderRepository, OrderFollowRepository orderFollowRepository) {
        this.zadOrderRepository = zadOrderRepository;
        this.orderFollowRepository = orderFollowRepository;
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void updateOrder() {
        // 昨天
        LocalDate now = LocalDate.now().plusDays(-1);
        Specification<ZadOrder> specification = ((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            Predicate timeStart = criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), now.atStartOfDay());
            predicates.add(timeStart);
            Predicate timeEnd = criteriaBuilder.lessThanOrEqualTo(root.get("createTime"), now.plusDays(1).atStartOfDay());
            predicates.add(timeEnd);
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        });
        List<ZadOrder> zadOrders = zadOrderRepository.findAll(specification);
        zadOrders.forEach(zadOrder -> {
            List<OrderFollow> orderFollows = orderFollowRepository.findByOrderId(zadOrder.getId());
            if (orderFollows.size() > 0) {
                OrderFollow orderFollow = orderFollows.get(0);
                // 判断是否超期
                if (zadOrder.getActDepartureTime() != null) {
                    boolean alert = check(orderFollow);
                    zadOrder.setAlert(alert);
                    zadOrderRepository.save(zadOrder);
                } else if (orderFollow.getRemindDate() != null && LocalDate.now().isAfter(orderFollow.getRemindDate())) {
                    boolean alert = check(orderFollow);
                    zadOrder.setAlert(alert);
                    zadOrderRepository.save(zadOrder);
                }
            }
        });
    }

    private boolean check(OrderFollow orderFollow) {
        return !"已审核完成".equals(orderFollow.getPackagePic()) || !"已审核完成".equals(orderFollow.getCustomsFile()) ||
                !"已审核完成".equals(orderFollow.getCarFile()) || !"已审核完成".equals(orderFollow.getPackageSolution()) ||
                !"已审核完成".equals(orderFollow.getStFile()) || !"已审核完成".equals(orderFollow.getPreOrderConfirm()) ||
                !"已审核完成".equals(orderFollow.getTransportOrder()) || !"已审核完成".equals(orderFollow.getCustomsReleaseOrder()) ||
                !"已审核完成".equals(orderFollow.getDskAtbMrn());
    }
}
